package com.chenchen.tiguan.venue.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.chenchen.tiguan.venue.domain.Venue;
import com.chenchen.tiguan.venue.domain.VenueClosingSlot;
import com.chenchen.tiguan.venue.service.IVenueClosingSlotService;
import com.chenchen.tiguan.venue.service.IVenueTimeSlotService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chenchen.tiguan.common.annotation.Log;
import com.chenchen.tiguan.common.core.controller.BaseController;
import com.chenchen.tiguan.common.core.domain.AjaxResult;
import com.chenchen.tiguan.common.enums.BusinessType;
import com.chenchen.tiguan.venue.domain.VenueOpenTime;
import com.chenchen.tiguan.venue.service.IVenueOpenTimeService;
import com.chenchen.tiguan.common.utils.poi.ExcelUtil;
import com.chenchen.tiguan.common.core.page.TableDataInfo;

/**
 * 场馆每周固定开放时段Controller
 * 
 * @author chenchen
 * @date 2025-04-14
 */
@RestController
@RequestMapping("/venue/opentime")
public class VenueOpenTimeController extends BaseController
{
    @Autowired
    private IVenueOpenTimeService venueOpenTimeService;
    @Autowired
    private IVenueTimeSlotService venueTimeSlotService;
    @Autowired
    private IVenueClosingSlotService venueClosingSlotService;

    /**
     * 查询场馆每周固定开放时段列表
     */
    @PreAuthorize("@ss.hasPermi('venue:opentime:list')")
    @GetMapping("/list")
    public TableDataInfo list(VenueOpenTime venueOpenTime,@Param("venueName") String venueName)
    {
        Venue venue = new Venue();
        venue.setVenueName(venueName);
        venueOpenTime.setVenue(venue);
        startPage();
        List<VenueOpenTime> list = venueOpenTimeService.selectVenueOpenTimeList(venueOpenTime);
        return getDataTable(list);
    }

    /**
     * 导出场馆每周固定开放时段列表
     */
    @PreAuthorize("@ss.hasPermi('venue:opentime:export')")
    @Log(title = "场馆每周固定开放时段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VenueOpenTime venueOpenTime)
    {
        List<VenueOpenTime> list = venueOpenTimeService.selectVenueOpenTimeList(venueOpenTime);
        ExcelUtil<VenueOpenTime> util = new ExcelUtil<VenueOpenTime>(VenueOpenTime.class);
        util.exportExcel(response, list, "场馆每周固定开放时段数据");
    }

    /**
     * 获取场馆每周固定开放时段详细信息
     */
    @PreAuthorize("@ss.hasPermi('venue:opentime:query')")
    @GetMapping(value = "/{openTimeId}")
    public AjaxResult getInfo(@PathVariable("openTimeId") Long openTimeId)
    {
        return success(venueOpenTimeService.selectVenueOpenTimeByOpenTimeId(openTimeId));
    }

    /**
     * 新增场馆每周固定开放时段
     */
    @PreAuthorize("@ss.hasPermi('venue:opentime:add')")
    @Log(title = "场馆每周固定开放时段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VenueOpenTime venueOpenTime)
    {
        int i = venueOpenTimeService.insertVenueOpenTime(venueOpenTime);
        venueTimeSlotService.updateVenueTimeSlotWhenInsertAndUpdate(venueOpenTime.getVenueId(),venueOpenTime);

        List<VenueClosingSlot> venueClosingSlots = venueClosingSlotService.selectVenueClosingSlotByVenueId(venueOpenTime.getVenueId());
        for (VenueClosingSlot venueClosingSlot : venueClosingSlots)
        {
            venueClosingSlotService.updateTimeSlotStatusWhenInsertAndUpdate(venueClosingSlot);
        }
        return toAjax(i);
    }

    /**
     * 修改场馆每周固定开放时段
     */
    @PreAuthorize("@ss.hasPermi('venue:opentime:edit')")
    @Log(title = "场馆每周固定开放时段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VenueOpenTime venueOpenTime)
    {
        int i = venueOpenTimeService.updateVenueOpenTime(venueOpenTime);
        venueTimeSlotService.updateVenueTimeSlotWhenInsertAndUpdate(venueOpenTime.getVenueId(),venueOpenTime);
        List<VenueClosingSlot> venueClosingSlots = venueClosingSlotService.selectVenueClosingSlotByVenueId(venueOpenTime.getVenueId());
        for (VenueClosingSlot venueClosingSlot : venueClosingSlots)
        {
            venueClosingSlotService.updateTimeSlotStatusWhenInsertAndUpdate(venueClosingSlot);
        }
        return toAjax(i);
    }

    /**
     * 删除场馆每周固定开放时段
     */
    @PreAuthorize("@ss.hasPermi('venue:opentime:remove')")
    @Log(title = "场馆每周固定开放时段", businessType = BusinessType.DELETE)
	@DeleteMapping("/{openTimeIds}")
    public AjaxResult remove(@PathVariable Long[] openTimeIds)
    {
        for (Long openTimeId : openTimeIds){
            VenueOpenTime venueOpenTime = venueOpenTimeService.selectVenueOpenTimeByOpenTimeId(openTimeId);
            venueTimeSlotService.updateVenueTimeSlotWhenDelete(venueOpenTime.getVenueId(),venueOpenTime);
        }
        int i = venueOpenTimeService.deleteVenueOpenTimeByOpenTimeIds(openTimeIds);
        return toAjax(i);
    }
}
